//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.dao.wills.PssPurcReceiveListDao;
import com.aidong.model.wills.Products;
import com.aidong.model.wills.PssPopProduct;
import com.aidong.model.wills.PssProductStock;
import com.aidong.model.wills.PssProductStockPK;
import com.aidong.model.wills.PssPurcReceive;
import com.aidong.model.wills.PssPurcReceiveImpl;
import com.aidong.model.wills.PssPurcReceiveList;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class PssPurcReceiveDao extends BaseDao<PssPurcReceive, Integer> {
    public PssPurcReceiveDao() {
    }

    public void addPurcReceive(PssPurcReceive pssPurcReceive, PssPurcReceiveListDao pssPurcReceiveListDao, PssProductStockDao pssProductStockDao, List<PssPurcReceiveList> pssPurcReceiveList) {
        this.add(pssPurcReceive);
        Iterator i$ = pssPurcReceiveList.iterator();

        while(i$.hasNext()) {
            PssPurcReceiveList pss = (PssPurcReceiveList)i$.next();
            pss.setReceiveID(pssPurcReceive.getReceiveID());
            pss.setAmount(Double.valueOf(pss.getPrice().doubleValue() * pss.getQuantity().doubleValue()));
            this.getSession().save(pss);
        }

    }

    public void updatePurcReceive(PssPurcReceiveListDao pssPurcReceiveListDao, PssProductStockDao pssProductStockDao, String shopCode, Products products) {
        Iterator i$ = products.getLstProduct().iterator();

        while(i$.hasNext()) {
            PssPopProduct pc = (PssPopProduct)i$.next();
            PssPurcReceiveList pssPurcReceiveList = (PssPurcReceiveList)pssPurcReceiveListDao.getById(pc.getId());
            if(Double.compare(pc.getQty().doubleValue(), pssPurcReceiveList.getQuantity().doubleValue()) != 0) {
                pssPurcReceiveList.setQuantity(pc.getQty());
                pssPurcReceiveList.setAmount(Double.valueOf(pssPurcReceiveList.getPrice().doubleValue() * pc.getQty().doubleValue()));
                pssPurcReceiveListDao.update(pssPurcReceiveList);
            }
        }

    }

    public void updatePurcReceiveForConfirm(PssPurcReceiveListDao pssPurcReceiveListDao, PssProductStockDao pssProductStockDao, Integer receiveID, String shopCode, String loginUserCode) {
        this.updateStatus(receiveID, 1);
        List list = pssPurcReceiveListDao.queryPurcReceiveList(receiveID);
        Iterator i$ = list.iterator();

        while(i$.hasNext()) {
            PssPurcReceiveList pr = (PssPurcReceiveList)i$.next();
            PssProductStockPK pssProductStockPK = new PssProductStockPK();
            pssProductStockPK.setProductCode(pr.getProductCode());
            pssProductStockPK.setShopCode(shopCode);
            PssProductStock pssProductStock = (PssProductStock)pssProductStockDao.getById(pssProductStockPK);
            if(pssProductStock == null) {
                pssProductStock = new PssProductStock();
                pssProductStock.setProductCode(pr.getProductCode());
                pssProductStock.setShopCode(shopCode);
                pssProductStock.setQuantity(pr.getQuantity());
                pssProductStock.setCreateUser(loginUserCode);
                pssProductStock.setCreateTime(new Date());
                pssProductStock.setUpdateTime(new Date());
                pssProductStockDao.add(pssProductStock);
            } else {
                pssProductStock.setQuantity(Double.valueOf(pssProductStock.getQuantity().doubleValue() + pr.getQuantity().doubleValue()));
                pssProductStock.setUpdateTime(new Date());
                pssProductStockDao.update(pssProductStock);
            }
        }

    }

    private boolean updateStatus(Integer receiveID, int status) {
        String hql = "update PssPurcReceive set status=:p1 where ReceiveID=:p2";
        return this.updateByHql(hql, new Object[]{Integer.valueOf(status), receiveID}) > 0;
    }

    public void getPurcReceiveRptList(Pagination<PssPurcReceiveImpl> pagination, ConditionMap cd) {
        ArrayList params = new ArrayList();
        String sqlw = cd.toSql(params);
        String sql = "select pr.ShopCode, pc.defaultContract, pr.oaOrderId, pr.ReceiveDate, pr.createuser, prl.ProviderCode, prl.ProductCode, pc.ProductName,prl.quantity,prl.amount  from PssPurcReceive pr  inner join PssPurcReceiveList prl on pr.ReceiveID=prl.ReceiveID  left outer  join PssProductCode pc on prl.productcode=pc.ProductCode  " + (sqlw.length() > 0?"where " + sqlw:"");
        sql = sql + " order by pr.ShopCode, pr.oaOrderId, prl.productcode ";
        this.queryPageListBySql(PssPurcReceiveImpl.class, pagination, sql, params.toArray());
    }
}
